package ext.tianma.part.export;

import ext.tianma.part.MyPartUtil;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.TreeMap;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import wt.enterprise.RevisionControlled;
import wt.part.LineNumber;
import wt.part.QuantityUnit;
import wt.part.Source;
import wt.part.WTPart;
import wt.part.WTPartUsageLink;
import wt.util.WTException;
import wt.vc.IterationIdentifier;
import wt.vc.VersionIdentifier;
import wt.vc.config.ConfigException;

public class BOMUtil
{
  private static final String NO_LINE_NUMBER = "0";

  public static String getSourceCN(WTPart wtpart)
    throws WTException
  {
    String sourceValue = "";

    if (wtpart != null) {
      Source source = wtpart.getSource();

      if (source != null) {
        sourceValue = source.getDisplay(Locale.CHINA);
      }
    }

    return sourceValue;
  }

  public static void getTemplateLocation(HSSFSheet sheet, HashMap map)
  {
	//得到该sheet页的第二行，下标从0开始
    HSSFRow row = sheet.getRow(1);
    //得到第二行的最后一个单元格的值（下标+1）
    int nSheetCol = row.getLastCellNum();
    Iterator keyIter = map.keySet().iterator();
    int z ;
    
    for(z = 0; z < nSheetCol && keyIter.hasNext();)
    //for (; keyIter.hasNext(),z < nSheetCol; )
    {
      Object key = keyIter.next();
      Object tableValue = map.get(key);
      if(z == 0){
    	  continue;
      }
      
      String cellValue = getExcelCellContent(sheet, 1, z);
      if ((key.equals(cellValue)) && 
        (!tableValue.equals(Integer.valueOf(z))))
      {
        map.put(key, Integer.valueOf(z));
      }
      z++;
    }
  }

  public static String getExcelCellContent(HSSFSheet sheet, int nRow, int nCol)
  {
    if ((sheet == null) || (nRow < 0) || (nCol < 0))
      return "";
    try {
      HSSFRow row = sheet.getRow(nRow);
      if (row == null)
        return "";
      HSSFCell aCell = row.getCell(nCol);
      if (aCell == null)
        return "";
      String content = aCell.getStringCellValue();
      if (content != null)
        content = content.trim();
      if (content == null);
      return "";
    }
    catch (Exception e) {
      System.out.println("\tgetExcelCellContent : error, nRow = '" + nRow + "' , nCol = '" + nCol + "'");
      e.printStackTrace();
    }
    return "";
  }

  public static String getVersionAndView(WTPart part)
  {
    String versionAndView = "";

    if (part == null) {
      return versionAndView;
    }

    String version = getVersion(part);

    String view = part.getViewName();

    if ((view != null) && (version != null)) {
      versionAndView = version + "(" + view + ")";
    }

    return versionAndView;
  }

  public static String getVersion(RevisionControlled revisionControlled)
  {
    String version = "";

    if (revisionControlled != null)
    {
      String majorVersion = revisionControlled.getVersionIdentifier().getValue();

      String minorVersion = revisionControlled.getIterationIdentifier().getValue();

      version = majorVersion + "." + minorVersion;
    }

    return version;
  }

  public static String getUnitEN(WTPart wtpart)
  {
    String defaultUnit = "";

    if (wtpart != null) {
      QuantityUnit quantityUnit = wtpart.getDefaultUnit();

      defaultUnit = getUnitEN(quantityUnit);
    }

    return defaultUnit;
  }

  public static String getUnitEN(QuantityUnit quantityUnit)
  {
    String defaultUnit = "";

    if (quantityUnit != null) {
      defaultUnit = quantityUnit.getLocalizedMessage(Locale.ENGLISH);
    }

    return defaultUnit;
  }

  public static String getLineNumber(WTPartUsageLink usageLink)
  {
    String lineNumberValue = "";

    if (usageLink != null) {
      LineNumber lineNumber = usageLink.getLineNumber();

      if (lineNumber != null) {
        lineNumberValue = String.valueOf(lineNumber.getValue());
      }
    }

    if (lineNumberValue.trim().equals("")) {
      lineNumberValue = "0";
    }

    return lineNumberValue;
  }

  public static List<WTPartUsageLink> getSortList(List<WTPartUsageLink> list)
  {
    List<WTPartUsageLink> usageLinkList = new ArrayList<WTPartUsageLink>();

    Hashtable<Integer,WTPartUsageLink> hashtable = new Hashtable<Integer,WTPartUsageLink>();

    if (list == null) {
      return null;
    }
    Iterator<WTPartUsageLink> iterator = list.iterator();

    while (iterator.hasNext()) {
      WTPartUsageLink usageLink = (WTPartUsageLink)iterator.next();

      if (usageLink != null) {
        String partLinkNumber = getLineNumber(usageLink);

        Integer lineNumber = Integer.valueOf(0);
        if ((partLinkNumber != null) && (!partLinkNumber.trim().isEmpty()) && (!partLinkNumber.trim().equals("0"))) {
          lineNumber = Integer.valueOf(Integer.parseInt(partLinkNumber));

          hashtable.put(lineNumber, usageLink);
        } else {
          usageLinkList.add(usageLink);
        }
      }
    }

    TreeMap treeMap = new TreeMap(hashtable);

    if ((treeMap != null) && (treeMap.size() > 0)) {
      Set set = treeMap.keySet();

      if (set != null) {
        Iterator iteratorKey = set.iterator();

        while (iteratorKey.hasNext()) {
          Integer key = (Integer)iteratorKey.next();

          WTPartUsageLink partUsageLink = (WTPartUsageLink)treeMap.get(key);

          if (partUsageLink != null) {
            usageLinkList.add(partUsageLink);
          }
        }
      }
    }
    return usageLinkList;
  }

  public static String getLineNumbers(WTPartUsageLink usageLink, int count)
  {
    String lineNumber = "";

    String number = getLineNumber(usageLink);
    String blank = "";
    for (int i = 0; i < count; i++) {
      blank = blank + "    ";
    }

    lineNumber = blank + number;

    return lineNumber;
  }

  public static String getPartNumber(WTPart part, int count)
    throws ConfigException, WTException
  {
    String partNumber = "";

    if (part != null) {
      String number = part.getNumber();

      List<WTPartUsageLink> list = MyPartUtil.getFirstLevelUsageLink(part);

      String blank = "";
      for (int i = 0; i < count; i++) {
        blank = blank + "    ";
      }

      if ((list == null) || (list.size() <= 0))
        partNumber = blank + number;
      else {
        partNumber = blank + "-" + number;
      }
    }
    return partNumber;
  }

  public static String getSubPartNumber(WTPart part, int count)
    throws ConfigException, WTException
  {
    String partNumber = "";

    if (part != null) {
      String number = part.getNumber();

      List list = MyPartUtil.getFirstLevelUsageLink(part);

      String blank = "";
      for (int i = 0; i < count; i++) {
        blank = blank + "    ";
      }

      if ((list == null) || (list.size() <= 0))
        partNumber = blank + number + "*";
      else {
        partNumber = blank + "-" + number + "*";
      }
    }
    return partNumber;
  }
}